let order = 1;
let total = 3;

let propId = null;
let requestUrl = null;

let operationArray = [null, {
	text: "&emsp;存入仓库&emsp;",
	iconCls: "icon-arrow_left",
	iconAlign: "left"
}, {
	text: "&emsp;取回包裹&emsp;",
	iconCls: "icon-arrow_right",
	iconAlign: "right"
}];

function addHandler() {
	let roleId = $("#role_id").combobox("getValue");

	if (roleId) {
		$("#roleId").combobox("setValue", roleId);
	}

	requestUrl = "/role_repository/insert";
	
	$("#repository_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#repository_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/role_repository/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#roleId").combobox("setValue", rowData.roleId);
		$("#location").numberspinner("setValue", rowData.location);

		$("#repository_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function deleteHandler() {
	let rowData = $("#repository_list").datagrid("getSelected");

	if (rowData) {
		$.messager.confirm("提示", "是否确认删除？", function (bool) {
			if (bool) {
				ajaxPost("/role_repository/deleteById", {
					id: rowData.id
				}, function (resp) {
					showMsg(resp.message);

					$("#repository_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要删除的记录！", "warning");
	}
}

function viewHandler() {
	let rowData = $("#repository_list").datagrid("getSelected");

	if (rowData) {
		let roleId = rowData.roleId;

		$("#role_list").datalist("reload", {
			callback: function () {
				$("#role_list").datalist("selectRecord", roleId);
			}
		});

		loadPackage(roleId);
		loadRepository(roleId);

		$("#repository_detail").dialog("open");
	} else {
		unselected();
	}
}

/**
 * 加载角色包裹
 * @param roleId 角色ID
 */
function loadPackage(roleId) {
	// 查询包裹格子
	ajaxGet("/package_grid/selectByRoleId", {
		roleId: roleId
	}, function (resp) {
		let list = resp.data;

		if (list && list.length > 0) {
			let content = "";

			content += "<tr>";

			for (let i = 0; i < list.length; i++) {
				let location = list[i].location;

				content += "<td id='package_grid_" + location + "'>";
				content += "<div class='s50'>";
				content += "<img height='48' src='/images/blank.png' class='package_grid_" + location + "' />";
				content += "<i></i>";
				content += "</div>";
				content += "</td>";

				// 5个格子换行
				if ((i + 1) % 5 === 0) {
					content += "</tr>";

					// 最后一个格子不用新起一行
					if (i < list.length - 1) {
						content += "<tr>";
					}
				}
			}

			$("#package").empty().append(content);

			// 查询包裹道具
			ajaxGet("/package_prop/selectByRoleId", {
				roleId: roleId
			}, function (resp) {
				let list = resp.data;

				if (list && list.length > 0) {
					for (let i = 0; i < list.length; i++) {
						let prop = list[i];

						// 给包裹的单元格绑定鼠标双击和右键点击事件
						$("#package > tr > td#package_grid_" + prop.location).draggable({ // 创建一个可拖动的元素
							//proxy: "clone",
							onStopDrag: function () {
								$(this).draggable("options").cursor = "move";
							}
						}).unbind("contextmenu").contextmenu(function (event) { // 绑定鼠标右键点击事件
							propId = $(this).attr("ref");

							if (propId) {
								event.preventDefault();

								ajaxGet("/prop/selectProp", {
									propId: propId
								}, function (resp) {
									let data = resp.data;
									let days = data.days;

									if (days !== 0) {
										$("#frozen").show();

										if (days < 0) {
											days = "永久";
										}

										$("#days").html(days);
									} else {
										$("#frozen").hide();
									}

									$("#prop_image").attr("src", data.image);
									$("#prop_name").html(data.name);
									$("#prop_note").html(data.note);

									let options = operationArray[data.state];

									$("#move").linkbutton({
										text: options.text,
										iconCls: options.iconCls,
										iconAlign: options.iconAlign
									});

									$("#prop_detail").dialog("open");
								}, error);
							}
						}).unbind("dblclick").dblclick(function () { // 双击移动
							// 得到当前道具的ID
							let propId = $(this).attr("ref");

							if (propId) {
								let repositoryId = $("#repository_name").combobox("getValue");

								ajaxPost("/prop/move", {
									propId: propId,
									repositoryId: repositoryId
								}, function (resp) {
									showMsg(resp.message);

									let rowData = $("#repository_list").datagrid("getSelected");

									if (rowData) {
										let roleId = rowData.roleId;

										loadPackage(roleId);
										loadRepository(roleId);

										let repositoryId = $("#repository_name").combobox("getValue");

										loadRepositoryGrid(repositoryId);
									} else {
										unselected();
									}
								}, error);
							}
						}).attr("ref", prop.id);

						// 设置单元格中的图片属性
						$("#package > tr > td > div.s50 > img.package_grid_" + prop.location).attr({
							src: prop.image,
							title: prop.name
						}).next("i").addClass( (prop.bindState === 2) ? "bind" : ( (prop.rarity === 1) ? "zhenpin" : "") );
					}
				}
			});
		}
	});
}

/**
 * 加载角色仓库
 * @param roleId 角色ID
 */
function loadRepository(roleId) {
	$("#repository_name").combobox("reload", {
		roleId: roleId
	});

	// 查询角色仓库
	ajaxGet("/role_repository/selectByRoleId", {
		roleId: roleId
	}, function(resp) {
		let data = resp.data;

		if (data && data.length > 0) {
			total = data.length;

			$("#total").html(total);

			// 选中第一个仓库
			$("#repository_name").combobox("setValue", data[0].id);
		}
	}, error);

	// if (repositories && repositories.length > 0) {
	// 	for (let i = 0; i < repositories.length; i++) {
	// 		let repository = repositories[i];
	//
	// 		// 选中开启的仓库
	// 		if (repository.openStatus === 1) {
	// 			$("#repository_name").combobox("setValue", repository.id);
	// 		}
	// 	}
	// }
}

/**
 * 加载仓库格子
 * @param repositoryId 仓库ID
 */
function loadRepositoryGrid(repositoryId) {
	// 查询仓库格子
	ajaxGet("/repository_grid/selectByRepositoryId", {
		repositoryId: repositoryId
	}, function (resp) {
		let list = resp.data;

		if (list && list.length > 0) {
			let content = "";

			content += "<tr>";

			for (let i = 0; i < list.length; i++) {
				let location = list[i].location;

				content += "<td id='repository_grid_" + location + "'>";
				content += "<div class='s50'>";
				content += "<img height='48' src='/images/blank.png' class='repository_grid_" + location + "' />";
				content += "<i></i>";
				content += "</div>";
				content += "</td>";

				// 5个格子换行
				if ((i + 1) % 5 === 0) {
					content += "</tr>";

					// 最后一个格子不用新起一行
					if (i < list.length - 1) {
						content += "<tr>";
					}
				}
			}

			$("#repository").empty().append(content);
		}

		// 通过仓库ID查询仓库道具
		ajaxGet("/repository_prop/selectByRepositoryId", {
			repositoryId: repositoryId
		}, function (resp) {
			let list = resp.data;

			if (list && list.length > 0) {
				for (let i = 0; i < list.length; i++) {
					let prop = list[i];

					$("#repository > tr > td#repository_grid_" + prop.location).draggable({ // 创建一个可拖动的元素
						//proxy: "clone",
						onStopDrag: function () {
							$(this).draggable("options").cursor = "move";
						}
					}).unbind("contextmenu").contextmenu(function (event) { // 绑定鼠标右键点击事件
						propId = $(this).attr("ref");

						if (propId) {
							event.preventDefault();

							ajaxGet("/prop/selectProp", {
								propId: propId
							}, function (resp) {
								let data = resp.data;

								if (data) {
									viewProp(data);
								}
							}, error);
						}
					}).unbind("dblclick").dblclick(function () { // 双击移动
						// 得到当前道具的ID
						let propId = $(this).attr("ref");

						if (propId) {
							ajaxPost("/prop/move", {
								propId: propId
							}, function (resp) {
								showMsg(resp.message);

								let rowData = $("#repository_list").datagrid("getSelected");

								if (rowData) {
									let roleId = rowData.roleId;

									loadPackage(roleId);
									loadRepository(roleId);

									let repositoryId = $("#repository_name").combobox("getValue");

									loadRepositoryGrid(repositoryId);
								} else {
									unselected();
								}
							}, error);
						}
					}).attr("ref", prop.id);

					$("#repository > tr > td > div.s50 > img.repository_grid_" + prop.location).attr({
						src: prop.image,
						title: prop.name
					}).next("i").addClass( (prop.bindState === 2) ? "bind" : ( (prop.rarity === 1) ? "zhenpin" : "") );
				}
			}
		}, error);
	}, error);
}

/**
 * 搜索道具
 */
function search(name) {
	if (!name) {
		name = $("#propName").textbox("getValue");
	}

	if (name) {
		let rowData = $("#role_list").datalist("getSelected");

		if (rowData) {
			let roleId = rowData.id;

			ajaxGet("/repository_prop/selectByName", {
				name: name,
				roleId: roleId
			}, function (resp) {
				let jsonPage = resp.data;
				let total = jsonPage.total;

				if (total > 0) {
					let rows = jsonPage.rows;

					$("#propName").textbox("setValue", name);
					$("#repository_count").html(rows.length);
					$("#result_count").html(total);

					let content = "";

					for (let i = 0; i < rows.length; i++) {
						let repisitoryProp = rows[i];
						let list = repisitoryProp.list;

						content += "<div>";
						content += "<div class='result_repo'>";
						content += "<span class='left'>"+ repisitoryProp.repositoryName + "</span>";
						content += "<span class='right'>";
						content += "<button type='button' onclick='openRepository(\"" + repisitoryProp.repositoryId + "\")'>打开</button>";
						content += "</span>";
						content += "</div>";

						content += "<div style='overflow-x:scroll;'>";
						content += "<table style='border-spacing:4px;'>";
						content += "<tbody>";
						content += "<tr class='search_result'>";

						if (list.length > 0) {
							for (let i = 0; i < list.length; i++) {
								let data = list[i];

								content += "<td ref='" + data.id + "' title='" + data.name + "'>"
								content += "<div>";

								if (data.bindState === 2) {
									content += "<i class='bind'></i>";
								}

								content += "<img src='" + data.image + "' />";

								// 图片的下标
								if (data.data) {
									content += "<span>" + subscript + "</span>";
								}

								content += "</div>"
								content += "</td>"
							}
						}

						content += "</tr>";
						content += "</tbody>";
						content += "</table>";
						content += "</div>";
						content += "</div>";
					}

					$(".result_panel").empty().append(content);
					$("#not_empty").show();
					$("#empty").hide();

					bindEvent();
				} else {
					let message = "少侠没有该物品哦！";

					alertMsg(message);

					$("#not_empty").hide();
					$("#empty").show();
				}
			}, error);
		}
	} else {
		$("#not_empty").hide();
		$("#empty").show();
	}
}

/**
 * 打开仓库
 */
function openRepository(repositoryId) {
	$("#repository_name").combobox("setValue", repositoryId);
}

/**
 * 绑定事件
 */
function bindEvent() {
	$(".search_result > td").unbind("contextmenu").contextmenu(function (event) { // 绑定鼠标右键点击事件
		propId = $(this).attr("ref");

		if (propId) {
			event.preventDefault();

			ajaxGet("/prop/selectProp", {
				propId: propId
			}, function (resp) {
				let data = resp.data;

				if (data) {
					viewProp(data);
				} else {
					alertMsg("找不到道具信息！");
				}
			}, error);
		}
	}).unbind("dblclick").dblclick(function () { // 绑定鼠标双击事件
		let _this = this;

		// 得到当前道具的ID
		let propId = $(_this).attr("ref");

		if (propId) {
			// 双击移动
			ajaxPost("/prop/move", {
				propId: propId
			}, function (resp) {
				showMsg(resp.message);

				let rowData = $("#repository_list").datagrid("getSelected");

				if (rowData) {
					let roleId = rowData.roleId;

					loadPackage(roleId);
					loadRepository(roleId);

					$(_this).attr({
						"title": "",
						"ref": ""
					}).children("div").empty();
				} else {
					unselected();
				}
			}, error);
		}
	});
}

/**
 * 查看道具
 * @param data 道具信息
 */
function viewProp(data) {
	let days = data.days;

	if (days !== 0) {
		if (days < 0) {
			days = "永久";
		}

		$("#frozen").show();
		$("#prop_days").html(days);
	} else {
		$("#frozen").hide();
	}

	$("#prop_name").html(data.name);
	$("#prop_note").html(data.note);
	$("#prop_type").html(data.type);
	$("#prop_quality").html(data.extraNote);
	$("#prop_image").attr("src", data.image);

	// 耐久度的展示
	let durability = data.durability;

	if (durability == null) {
		$("#durability").hide();
		$("#prop_durability").empty();
	} else {
		$("#durability").show();
		$("#prop_durability").html(data.durability);
	}

	let options = operationArray[data.state];

	$("#move").linkbutton({
		text: options.text,
		iconCls: options.iconCls,
		iconAlign: options.iconAlign
	});

	$("#prop_detail").dialog("open");
}

$(document).ready(function() {

	$("#role_id").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-选择角色-",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#modeling_" + row.id).attr({
					"src": data.image,
					"title": data.name
				});
			}, error);

			return "<img width='18' id='modeling_" + row.id + "' />&nbsp;" + row.name;
		}
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let roleId = $("#role_id").combobox("getValue");

		$("#repository_list").datagrid("reload", {
			roleId: roleId
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#name").textbox({
		width: 100,
		required: true
	});
	
	$("#location").numberspinner({
        min: 1,
        max: 25,
        width: 100,
        editable: true,
        required: true
    });

	$("#roleId").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#role_category_" + row.id).attr({
					title: data.name,
					src: data.image,
					alt: data.name
				});
			}, error);

			return "<img width='18' id='role_category_" + row.id + "' />&nbsp;" + row.name;
		}
	});
	
	$("#repository_dialog").dialog({
		title: "仓库信息",
		width: 400,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#repository_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#repository_dialog").dialog("close");
						$("#repository_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#repository_form").form("clear");
				$("#repository_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	$("#repository_name").combobox({
		url: "/role_repository/selectByRoleId",
		valueField: "id",
		textField: "name",
		width: 120,
		panelHeight: "auto",
		onSelect: function (record) {
			if (record) {
				order = record.location;

				$("#current").html(order);

				loadRepositoryGrid(record.id);
			}
		},
		onLoadError: function () {
			console.log("数据加载失败...");
		},
		onLoadSuccess: function () {
			let data = $(this).combobox("getData");
			let rowData = $("#role_list").datalist("getSelected");

			console.log(data.length === 0);
			console.log(rowData != null);

			if (data.length === 0 && rowData != null) { // 防止第一次访问页面就提示
				let message = "未查询到角色的仓库信息，是否初始化角色仓库？";

				$.messager.confirm("系统提示", message, function(bool) {
					if (bool) {

						let rowData = $("#role_list").datalist("getSelected");

						if (rowData) {
							let roleId = rowData.id;

							$.messager.prompt("提示信息", "请输入仓库数量：", function(count) {
								if (count) {
									if ($.isNumeric(count)) {
										ajaxPost("/role_repository/init", {
											roleId: roleId,
											count: count
										}, function () {
											ajaxPost("/role_package/init", {
												roleId: roleId
											}, function (resp) {
												$.messager.show({
													title: "系统消息",
													msg: resp.message,
													timeout: 5000,
													showType: "slide"
												});
											});
										});
									} else {
										$.messager.alert("提示信息", "请输入一个数字！", "warning");
									}
								}
							});
						}
					}
				});
			}
		}
	});

	$("#edit").linkbutton({
		iconCls: "icon-edit",
		onClick:function() {
			$.messager.prompt("仓库重命名", "请输入新仓库名:", function (name) {
				if (name) {
					let repositoryId = $("#repository_name").combobox("getValue");

					ajaxPost("/role_repository/updateNameById", {
						id: repositoryId,
						name: name
					}, function (resp) {
						showMsg(resp.message);

						$("#repository_name").combobox("setValue", repositoryId);
					})
				}
			});
		}
	});

	$("#query").linkbutton({
		iconCls: "icon-search",
		onClick: function() {
			// 获取#repository_detail > easyui-layout #layout-east坐标
			let offest = $("#layout-east").offset();

			$("#not_empty").hide();
			$("#empty").show();

			$("#search_dialog").dialog("open").dialog("move", {
				left: offest.left,
				top: offest.top
			});
		}
	});

	$("#role_list").datalist({
		url: "/role_account/selectAll",
		idField: "id",
		valueField: "id",
		textField: "name",
		width: 168,
		height: 369,
		textFormatter: function(value, rowData, rowIndex) {
			let html = "";

			html += "<table style='border-spacing:0;'>";
			html += 	"<tr>";
			html += 		"<td rowspan=2>";
			html += 			"<div style='width:47px;height:47px;'>";
			html += 				"<img width='45' id='role__" + rowData.id + "' />";
			html += 			"</div>";
			html += 		"</td>";

			html += 		"<td colspan=2>";
			html += 			"<span>" + rowData.name + "</span>";
			html += 		"</td>";
			html += 	"</tr>";

			html += 	"<tr>";
			html += 		"<td>";
			html += 			"<img width='20' id='school_" + rowData.id + "' />";
			html += 		"</td>";

			html += 		"<td>";
			html += 			"<span>" + rowData.grade + "级</span>";
			html += 		"</td>";
			html += 	"</tr>";
			html += "</table>";

			ajaxGet("/role_category/selectById", {
				id: rowData.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#role__" + rowData.id).attr("src", data.image)
					.attr("title", data.name);
			}, error);

			ajaxGet("/school/selectById", {
				id: rowData.schoolId
			}, function(resp) {
				let data = resp.data;

				$("#school_" + rowData.id).attr("src", data.image)
					.attr("title", data.name);
			}, error);

			return html;
		},
		// onBeforeSelect: function(rowIndex, rowData) { // 阻止选中未初始化仓库的角色
		// 	let flag = false;
		//
		// 	ajaxGet("/role_repository/selectByRoleId", {
		// 		roleId: rowData.id
		// 	}, function (resp) {
		// 		let list = resp.data;
		//
		// 		if (list && list.length > 0) {
		// 			flag = true;
		// 		}
		// 	}, error, false);
		//
		// 	return flag;
		// },
		onSelect: function(rowIndex, rowData) {
			if(rowData) {
				let roleId = rowData.id;

				loadPackage(roleId);
				loadRepository(roleId);
			}
		}
	});

	/**
	 * 切换到上一个仓库
	 */
	$("#pre").linkbutton({
		iconCls: "icon-arrow_up",
		onClick: function() {
			if (order > 1) {
				-- order;
			} else {
				order = total;
			}

			let rows = $("#repository_name").combobox("getData");

			$('#repository_name').combobox("setValue", rows[order - 1].id);
			// for (let i = 0; i < rows.length; i++) {
			// 	let row = rows[i];
			//
			// 	if (row.location === order) {
			// 		$('#repository_name').combobox("setValue", row.id);
			// 		break;
			// 	}
			// }
		}
	});

	/**
	 * 切换到下一个仓库
	 */
	$("#next").linkbutton({
		iconCls: "icon-arrow_down",
		onClick: function() {
			if (order < total) {
				++ order;
			} else {
				order = 1;
			}

			let rows = $("#repository_name").combobox("getData");

			$('#repository_name').combobox("setValue", rows[order - 1].id);
			// for (let i = 0; i < total; i++) {
			// 	let row = rows[i];
			//
			// 	if (row.location === order) {
			// 		$('#repository_name').combobox("setValue", row.id);
			// 		break;
			// 	}
			// }
		}
	});

	/**
	 * 整理仓库
	 */
	$("#arrange_repository").linkbutton({
		text: "&emsp;整理仓库&emsp;",
		onClick: function() {
			// 如果当前选中的角色没有初始化仓库，会导致整理操作无效（因为角色和当前仓库的归属不一致）
			let rowData = $("#role_list").datalist("getSelected");

			if (rowData) {
				let roleId = rowData.id;

				ajaxPost("/repository_prop/arrange", {
					roleId: roleId
				}, function (resp) {
					showMsg(resp.message);

					let rowData = $("#repository_list").datagrid("getSelected");

					if (rowData) {
						let roleId = rowData.roleId;

						loadRepository(roleId);

						let repositoryId = $("#repository_name").combobox("getValue");

						loadRepositoryGrid(repositoryId);
					} else {
						unselected();
					}
				}, error);
			}
		}
	});

	/**
	 * 创建一个可放置的元素
	 */
	$("#repository").droppable({
		accept: "#package > tr > td", // 可接收的元素
		onDragEnter: function(e, source) {
			$(source).draggable("options").cursor = "move";
		},
		onDragLeave: function(e, source) {
			$(source).draggable("options").cursor = "not-allowed";
		},
		onDrop: function(e, source) { // 放入到放置区
			// 得到当前道具的ID
			let propId = $(source).attr("ref");

			if (propId) {
				let repositoryId = $("#repository_name").combobox("getValue");

				ajaxPost("/prop/move", {
					propId: propId,
					repositoryId: repositoryId
				}, function (resp) {
					showMsg(resp.message);

					let rowData = $("#repository_list").datagrid("getSelected");

					if (rowData) {
						let roleId = rowData.roleId;

						loadPackage(roleId);
						loadRepository(roleId);
					} else {
						unselected();
					}
				}, error);
			}
		}
	});

	$("#tabs").tabs({
		fit: true,
		pill: true,
		narrow: false,
		border: false,
		closable: false,
		justified: true,
		showHeader: true
	});

	/**
	 * 创建一个可放置的元素
	 */
	$("#package").droppable({
		accept: "#repository > tr > td", // 可接收的元素
		onDrop: function(e, source) { // 放入到放置区
			// 得到当前道具的ID
			let propId = $(source).attr("ref");

			ajaxPost("/prop/move", {
				propId: propId
			}, function (resp) {
				showMsg(resp.message);

				let rowData = $("#repository_list").datagrid("getSelected");

				if (rowData) {
					let roleId = rowData.roleId;

					loadPackage(roleId);
				} else {
					unselected();
				}
			}, error);
		},
		onDragEnter: function(e, source) {
			$(source).draggable("options").cursor = "move";
		},
		onDragLeave: function(e, source) {
			$(source).draggable("options").cursor = "not-allowed";
		}
	});

	$("#arrange_package").linkbutton({
		text: "&emsp;整理包裹&emsp;"
	}).click(function() {
		let rowData = $("#role_list").datalist("getSelected");

		if (rowData) {
			let roleId = rowData.id;

			ajaxPost("/package_prop/arrange", {
				roleId: roleId
			}, function (resp) {
				showMsg(resp.message);

				let rowData = $("#repository_list").datagrid("getSelected");

				if (rowData) {
					let roleId = rowData.roleId;

					loadPackage(roleId);
					loadRepository(roleId);
				} else {
					unselected();
				}
			}, error);
		}
	});

	$("#repository_detail").dialog({
		title: " ",
		modal: false,
		closable: true,
		draggable: false
	});

	/******************************************************************************************************/

	$("#propName").textbox({
		width: 200,
		prompt: "请输入物品名称进行搜索",
		icons: [{
			iconCls: "icon-cancel",
			handler: function(event) {
				$(event.data.target).textbox("clear");

				$("#not_empty").hide();
				$("#empty").show();
			}
		}],
		inputEvents: {
			blur: function (event) {
				let target = $(event.data.target);
				let opts = target.textbox("options");

				if (target.textbox("getValue") !== opts.value) {
					target.textbox("setValue", opts.value);
				}
			},
			/**
			 * 键盘回车事件
			 */
			keydown: function (event) {
				// 如果不加下面这两行代码，失去焦点才会触发这个事件
				let target = $(event.data.target);

				target.textbox("setValue", target.textbox("getText"));

				if(event.keyCode === 13) {
					event.preventDefault();

					search();
				}
			}
		}
	});

	$("#exit_search").linkbutton({
		text: "&ensp;退出搜索&ensp;"
	}).click(function() {
		$(".result_panel").empty();
		// 清空搜索框的道具名称
		$("#propName").textbox("clear");
		$("#search_dialog").dialog("close");
	});

	$("#hot_words table > tbody > tr > td > button").on("click", function () {
		let name = $(this).children("div").eq(0).html();

		search(name);
	});

	$("#search_dialog").dialog({
		title: "",
		width: 320,
		height: 372,
		modal: false,
		cloned: false,
		draggable: false
	});

	/******************************************************************************************************/

	$("#prop_detail .splitter").textbox({
		width: 200
	});

	let options = operationArray[1];

	$("#move").linkbutton({
		text: options.text,
		iconCls: options.iconCls,
		iconAlign: options.iconAlign,
		onClick: function () {
			if (propId) {
				let repositoryId = $("#repository_name").combobox("getValue");

				ajaxPost("/prop/move", {
					propId: propId,
					repositoryId: repositoryId
				}, function (resp) {
					showMsg(resp.message);

					let rowData = $("#repository_list").datagrid("getSelected");

					if (rowData) {
						let roleId = rowData.roleId;

						loadPackage(roleId);
						loadRepository(roleId);

						// 清空道具格子
						$(".search_result > td[ref='"+ propId + "']").attr({
							"title": "",
							"ref": ""
						}).children("div").empty();

						/**
						 * 操作完成：清空道具ID的值，关闭对话框
						 */
						propId = null;

						$("#prop_detail").dialog("close");
					} else {
						unselected();
					}
				}, error);
			}
		}
	});

	$("#prop_detail").dialog({
		title: " ",
		width: 220,
		modal: true,
		closable: true
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_role_details * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#repository_list").datagrid({
		url: "/role_repository/selectByPage",
		height: height_table,
		rownumbers: true,
		fitColumns: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}, "-", {
			iconCls: "icon-search",
			text: "查看仓库",
			handler: function() {
				viewHandler();
			}
		}],
		columns: [[
			{field: "id", title: "仓库编号", align:"center", width: 160},
			{field: "name", title: "仓库名称", align:"center", width: 160},
			{field: "location", title: "仓库位置", align:"center", width: 160},
			roleDetailsColumn
		]]
	});

});